#ifndef RECORD_H
#define RECORD_H

#include "BaseGlobal.h"
#include "BaseStruct.h"

#include <QObject>
#include <QVariant>
#include <QVector>
#include <QMap>

BASE_EXPORT QDateTime getServerTime();
BASE_EXPORT bool execSQL(const QString & SQL, bool bOther = false);
BASE_EXPORT bool execSQL(const QString & SQL, QString & Msg, bool bOther = false);
BASE_EXPORT bool execSQL(const QStringList & SQL, QString & Msg, bool bOther = false);

//======================================================================================================================
class BASE_EXPORT QRecord : public QObject
{
	Q_OBJECT

public:
	QRecord(QObject *parent = 0);
	~QRecord();

public:
	bool select(const QString & SQL);
	bool select(const QString & SQL, QString & Error);

public:
	int rowCount() const;
	int colCount() const;
	short getCol(const QString & name) const;
	QString getColName(int col) const;
	QString getColAlias(int col) const;
	QList<ColumnInfo> & getColumns() const;
	QList<QVariantList> & getRows() const;

	QVariant data(int row, int col) const;
	QVariant data(int row, const QString & name) const;

	int getCollectInt(int row, int col) const;
	int getCollectInt(int row, const QString & name) const;
	QString getCollectStr(int row, int col) const;
	QString getCollectStr(int row, const QString & name) const;

public:
	virtual void clear();
	void wipeData();
	void reserve(int maxRows, int maxCols);

public:
	//addCol will clear table data
	void addCol(const ColumnInfo & col);
	void addRow(const QList<QVariant> & row);
	void removeRow(int row);

public:
	void createIndexOnCol(short col);
	void createIndexOnCol(const QString & name);

	QVariantList getIndexOnCol(short col) const;
	QVariantList getIndexOnCol(const QString & name) const;

	int getValueCountOnCol(short col, const QVariant & value) const;
	int getValueCountOnCol(const QString & name, const QVariant & value) const;

	QList< int > getIndexRowOnCol(short col, const QVariant & value) const;
	QList< int > getIndexRowOnCol(const QString & name, const QVariant & value) const;

	int getFetchRow(short col, const QVariant & value, int start = 0);
	int getFetchRow(const QString & name, const QVariant & value, int start = 0);
	int getFetchString(short col, const QString & value, int start = 0);
	int getFetchString(const QString & name, const QString & value, int start = 0);
	int getNotFetchRow(short col, const QVariant & value, int start = 0);
	int getNotFetchRow(const QString & name, const QVariant & value, int start = 0);

protected:
	//Indexes
	QMap<int, QVariantList> mapIndexes;
	mutable QList<ColumnInfo> listColumns;
	mutable QList<QVariantList> listRows;

};

#endif // RECORD_H
